package app.controllers.admin.activity;

import app.Const;
import app.kits.JobEhcacheKit;
import app.models.DaoKit;
import app.models.activity.MallActivity;
import app.models.info.ShoppingMall;
import com.github.sog.controller.BasicController;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.libs.AppFunc;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Strings;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import org.apache.commons.lang3.BooleanUtils;
import org.joda.time.DateTime;

import java.sql.Timestamp;
import java.util.List;

/**
 * <p>
 * 商场活动管理
 * </p>
 *
 * @author liuhui
 * @version 1.0 2014-04-15 14:05
 * @since JDK 1.6
 */
public class ActivityMallController extends BasicController{

    public void index() {
        render("activityMall_index.ftl");
    }


    public void list() {
        final DatatablesCriterias criterias = getCriterias();
        String name = getPara("name");
        Page<MallActivity> pageRt = MallActivity.dao.datatable(criterias,name);

        renderDataTables(pageRt, criterias);
    }


    public void create() {
        MallActivity mallActivity = new MallActivity();
        setAttr("activity", mallActivity);
        setAttr("mallblock", ShoppingMall.dao.malllist());
        setAttr(Const.ACTION_ATTR, Const.CREATE_ACTION);
        render("activityMall_item.ftl");
    }


    public void edit() {
        MallActivity activity = MallActivity.dao.findById(getPara());

        String pictures = activity.getStr("picture");
        if(!Strings.isNullOrEmpty(pictures)){
            final List<String> pics = AppFunc.COMMA_SPLITTER.splitToList(pictures);
            setAttr("pics", pics);
        }

        setAttr("activity", activity);
        setAttr("mallblock", ShoppingMall.dao.malllist());
        setAttr(Const.ACTION_ATTR, Const.EDIT_ACTION);
        render("activityMall_item.ftl");
    }

    public void remove() {
        String remove_ids = getPara(Const.DATA_PARAM);
        List<String> ids = AppFunc.COMMA_SPLITTER.splitToList(remove_ids);
        if (DaoKit.batch_remove(SqlKit.sql("mallactivity.remove"), ids)) {
            for (String id : ids) {
                CacheKit.remove(JobEhcacheKit.MALL_ACTIVITY_CACHE_NAME,JobEhcacheKit.MALL_ACTIVITY_KEY+id);
            }
            renderSuccess();
        } else {
            renderError("删除出现错误");
        }
    }

    public void save() {
        MallActivity activity = getModel(MallActivity.class,"activity");
        activity.set("reply", BooleanUtils.toBooleanObject(getPara("reply", "off")));
        activity.set("is_enroll", BooleanUtils.toBooleanObject(getPara("enroll", "off")));
        if (DaoKit.isNew(activity)) {
            activity.set("create_time",new Timestamp(DateTime.now().toDate().getTime()));
            activity.set("state",true);
            activity.save();
        } else {
            CacheKit.remove(JobEhcacheKit.MALL_ACTIVITY_CACHE_NAME, JobEhcacheKit.MALL_ACTIVITY_KEY + activity.get(AppFunc.TABLE_PK_COLUMN));
            activity.update();
        }
        //加入缓存
        CacheKit.put(JobEhcacheKit.MALL_ACTIVITY_CACHE_NAME,JobEhcacheKit.MALL_ACTIVITY_KEY+activity.get(AppFunc.TABLE_PK_COLUMN),activity);
        redirect("/admin/activity/activityMall");
    }

    public void stop(){
        String remove_ids = getPara(Const.DATA_PARAM);
        List<String> ids = AppFunc.COMMA_SPLITTER.splitToList(remove_ids);

        if (DaoKit.batch_update_by_id(SqlKit.sql("mallactivity.stop"),ids)) {
            for (String id : ids) {
                CacheKit.remove(JobEhcacheKit.MALL_ACTIVITY_CACHE_NAME,JobEhcacheKit.MALL_ACTIVITY_KEY+id);
            }
            renderSuccess();
        } else {
            renderError("停用出现错误");
        }
    }

    public void run(){
        String remove_ids = getPara(Const.DATA_PARAM);
        List<String> ids = AppFunc.COMMA_SPLITTER.splitToList(remove_ids);

        if (DaoKit.batch_update_by_id(SqlKit.sql("mallactivity.run"),ids)) {
            for (String id : ids) {
                MallActivity activity = MallActivity.dao.findById(id);
                CacheKit.put(JobEhcacheKit.MALL_ACTIVITY_CACHE_NAME,JobEhcacheKit.MALL_ACTIVITY_KEY+id,activity);
            }
            renderSuccess();
        } else {
            renderError("启用出现错误");
        }
    }

    /**
     * 禁用活动报名
     */
    public void stopEnroll(){
        String remove_ids = getPara(Const.DATA_PARAM);
        List<String> ids = AppFunc.COMMA_SPLITTER.splitToList(remove_ids);

        if (DaoKit.batch_update_by_id(SqlKit.sql("mallactivity.stop-enroll"),ids)) {
            renderSuccess();
        } else {
            renderError("禁用出现错误");
        }
    }

    /**
     * 启用活动报名
     */
    public void runEnroll(){
        String remove_ids = getPara(Const.DATA_PARAM);
        List<String> ids = AppFunc.COMMA_SPLITTER.splitToList(remove_ids);

        if (DaoKit.batch_update_by_id(SqlKit.sql("mallactivity.run-enroll"),ids)) {
            renderSuccess();
        } else {
            renderError("启用出现错误");
        }
    }


}
